/*头文件声明*/
#include"Key.h"
#include"Seg.h"
#include"LED.h"
#include <REGX52.H>

/*变量声明区*/
unsigned char Key_SlowDown;/*按键减速变量*/
unsigned char Key_Val,Key_Down,Key_UP,Key_Old;/*按键消抖*/
unsigned char Seg_SlowDown;/*数码管减速函数*/
unsigned char Seg_Disp[6]={10,10,10,10,10,10};/*数码管显示数组*/
unsigned char SegPoint_Disp[6]={0,0,0,0,0,0};
unsigned char Seg_Disp_Index;
unsigned char Seg_Mode;/*0-电压采集 1-数据显示 2-参数设置 3-计数统计*/
bit Twinkle_Flag;
unsigned int Time500ms;
unsigned char Data_Input[4]={14,14,14,14};
unsigned char Data_OldState[4];
unsigned char Data_OldState_Flag;
unsigned char Data_Input_Index;
float Votage;
unsigned int tmp1,num1,sbr1;
unsigned int tmp2,num2,sbr2;
double Votage_Set=3.00;
unsigned char Error_Counter;
unsigned char Counter;
unsigned char Led_Flag;
unsigned char Votage_Flag;
unsigned char Flag;
unsigned int Delay500ms;
/*按键处理函数*/
void Key_Proc()
{
	unsigned char Votage_Init=0;/*循环变量*/
	/*按键减速区*/
	if(Key_SlowDown)return;
	Key_SlowDown=1;
	/*按键消抖*/
	Key_Val=Key_write();
	Key_Down=Key_Val&(Key_Val^Key_Old);//按下有效
	Key_UP=~Key_Val&(Key_Val^Key_Old);//抬手有效
	Key_Old=Key_Val;
	if(Key_Down>0&&Key_Down<11)//如果按键按下
{
		/*有效操作*/
		if(Data_Input_Index<4&&Seg_Mode==0)
		{
			Data_Input[Data_Input_Index]=Key_Down-1;	
			Data_Input_Index++;
			Led_Flag=0;
			Error_Counter=0;
		}
		
		/*无效操作*/
		else
		{
				if(++Error_Counter>3)
				Led_Flag=1;
		}
}

		/*当实际电压小于电压设置参数状态*/
			if(Votage>Votage_Set)
		{
			/*从上往下时标志位拉高*/
				Votage_Flag=1;
		}
		/*当小于时Counter加加然后拉低标志位*/
		if(Votage_Flag==1&&Votage<Votage_Set)
			{
				Counter++;
				/*标志位拉低*/
			Votage_Flag=0;
			}
		
	/*数据处理区*/
	switch(Key_Down)
	{
		case 11:
			/*不在电压采集界面时重新进行一次电压采集*/
		if(Seg_Mode!=0)
		{
				for(Votage_Init=0;Votage_Init<4;Votage_Init++)
			{
				Data_Input[Votage_Init]=14;
			}
			Data_Input_Index=0;
			Seg_Mode=0;
		}
		/*在电压采集界面*/
		else
		{
			/*符合规范*/
			if(Data_Input[3]!=14)
			{
				Seg_Mode=1;
				Error_Counter=0;
			}
			/*如果不符合规范*/
			if(Data_Input[3]==14)
			{
				for(Votage_Init=0;Votage_Init<4;Votage_Init++)
				{
					Data_Input[Votage_Init]=14;
				}
					Data_Input_Index=0;
				Error_Counter++;
			}		
		
		}
		break;
			/*界面切换按键*/
		case 12:
			if(Seg_Mode)
			{
				Seg_Mode++;	
			if(Seg_Mode==4)
				Seg_Mode=1;
			}
		break;
	
		case 14:
			if(Seg_Mode==0)
	{
			for(Votage_Init=0;Votage_Init<4;Votage_Init++)
		{
		Data_Input[Votage_Init]=14;
		}
		Data_Input_Index=0;
	}
	/*计数清零*/
	if(Seg_Mode==3)
		Error_Counter=0;
		break;
	/*电压参数加按键*/
		case 15:
			if(Seg_Mode==2)
			{
					Votage_Set+=0.5;
				if(Votage_Set>6.00)	
				Votage_Set=1.00;
			}
		 break;
		case 16:	
			if(Seg_Mode==2)
		{
			Votage_Set-=0.5;
			if(Votage_Set<1.00)	
			Votage_Set=6.00;
		}
		break;
	}
	
}


/*数据处理区*/
void Seg_Proc()
{
	unsigned char i=0;/*循环变量*/
	static unsigned char j=1;/*高位熄灭变量*/
	/*数码管减速*/
	if(Seg_SlowDown)return;
	Seg_SlowDown=1;
		/*数码管函数区*/
	switch(Seg_Mode)
	{
		/*电压采集界面*/
		case 0:
			/*此处用一个指针会很难写*/
		/*刷新数码管数据*/
			Seg_Disp[0]=10;
			Seg_Disp[1]=10;
			SegPoint_Disp[3+(int)(Votage/10)]=10;
			if(Twinkle_Flag==1)
	{
		/*控制位显示*/
		for(i=0;i<4;i++)
		{
			Seg_Disp[2+i]=Data_Input[i];
		}
	}
	else
	{
		/*控制位闪烁*/
		Seg_Disp[Data_Input_Index+2]=10;
	}
		break;
		/*数据显示界面*/
		case 1:
		Seg_Disp[2]=10;
		Seg_Disp[3]=10;
		Seg_Disp[4]=10;
		Seg_Disp[5]=10;
		Seg_Disp[0]=15;/*符号位*/
		//此处一定记得除以1000.0(否则不会出现小数)
		Votage=(Data_Input[0]*1000+Data_Input[1]*100+Data_Input[2]*10+Data_Input[3]*1+5)/1000.0;
		SegPoint_Disp[3+(int)(Votage/10)]=1;
		/*浮点数的显示*/
		sbr1=(unsigned int)(Votage*1000)/1000;
		tmp1=(unsigned int)(Votage*1000)/100%10;
		num1=(unsigned int)(Votage*1000)/10%10;
		if(Votage>=10)
			Seg_Disp[3]=1;
		else
		{
		Seg_Disp[3]=sbr1;
		}
		Seg_Disp[4]=tmp1;
		Seg_Disp[5]=num1;
		break;
		/*参数设置界面*/
		case 2:
		Seg_Disp[2]=10;
		Seg_Disp[3]=10;
		Seg_Disp[4]=10;
		Seg_Disp[5]=10;
		Seg_Disp[0]=16;/*提示位*/
		sbr2=(unsigned int)(Votage_Set*1000)/1000;	
		tmp2=(unsigned int)(Votage_Set*1000)/100%10;
		num2=(unsigned int)(Votage_Set*1000)/10%10;
		Seg_Disp[3]=sbr2;
		Seg_Disp[4]=tmp2;
		Seg_Disp[5]=num2;
		break;
		/*计数统计界面*/
		case 3:
		Seg_Disp[0]=17;/*标志位*/
		Seg_Disp[2]=10;
		Seg_Disp[3]=10;
		Seg_Disp[4]=10;
		Seg_Disp[5]=10;	
		SegPoint_Disp[3+(int)(Votage/10)]=0;//熄灭小数点
		if(Counter<10)
		Seg_Disp[5]=Counter;
		if(Counter>10&&Counter<100)
		{
			Seg_Disp[4]=Counter/10%10;
			Seg_Disp[5]=Counter%10;
		}
		if(Counter>100)
		{
				Seg_Disp[3]=Counter/100%10;
			Seg_Disp[4]=Counter/10%10;
			Seg_Disp[5]=Counter%10;
		}
		/*此处要用的高位熄灭*/
		/*赋一个初值*/
			j=1;
		while(Seg_Disp[j]==0)
		{
				j++;
			Seg_Disp[j]=10;
		}
		break;

	}

}


/*其它函数处理区*/
void Led_Proc()
{
	if(Led_Flag==1)
	Led_Serve(2,1);
else
{
Led_Serve(2,0);
}
	/*计数值为奇数时*/
	if(Counter%2==1)
	Led_Serve(1,1);
	else
	{
		Led_Serve(1,0);
	}

/*当实际电压小于电压设置参数状态持续超过五秒*/
	Flag=Votage<Votage_Set?1:0;
if(Delay500ms>=500&&Seg_Mode!=0)
	Led_Serve(0,1);
else
{
	Led_Serve(0,0);
}
}

/*定时器初始化*/
void Timer0_Init()
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	EA=1;
	ET0=1;
}

/*定时器服务函数*/
void Timer0_Serve() interrupt 1
{
	TL0 = 0x18;
	TH0 = 0xFC;	
if(++Seg_SlowDown==500)Seg_SlowDown=0;
if(++Key_SlowDown==10)Key_SlowDown=0;	

Seg_Serve(Seg_Disp_Index,Seg_Disp[Seg_Disp_Index],SegPoint_Disp[Seg_Disp_Index]);
Seg_Disp_Index++;
Seg_Disp_Index%=6;
	
if(++Time500ms==500)
{
Twinkle_Flag^=1;
Time500ms=0;
}
if(Flag==1)
Delay500ms++;
else
{
	Delay500ms=0;
}
}

/*主函数*/
int main()
{

Timer0_Init();
while(1)
{
	Key_Proc();
	Seg_Proc();
	Led_Proc();
}

}